<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\App;
use App\Ad;
use App\Image;
use Illuminate\Support\Facades\Storage;

class AppController extends Controller
{
    public function AppUplaod(Request $request)
    {
        $token  = md5(date('Y-m-d',time())).md5('ldwl');
        if($token==$request->token){
            $map = $request->input();
            $file=$request->file('file');
            $path= $request->file->storeAs('public/images',md5(time()).'.'.$file->getClientOriginalExtension());
            $map['file'] = Storage::url($path);
            $map['app_size'] = Storage::size($path);
            $rew = App::create($map);
            if($rew){
                return $this->response->array([
                    'code'=>200,
                    'status'=>true,
                    'msg'=>'上传成功',
                ]);
            }
        }else{
            return $this->response->array([
                'code'=>400,
                'status'=>false,
                'msg'=>'token失效',
            ]);
        }
    }


    public function AppDownload(Request $request)
    {
        $data = App::OrderBy('created_at','desc')->first();
        return $this->response->array([
            'code'=>200,
            'status'=>true,
            'data'=>[
                'url'=>$data['file']
            ],
        ]);
    }


    public function AppChexTest()
    {
        $data = App::OrderBy('created_at','desc')->first();
        unset($data['token']);
        return $this->response->array([
            'code'=>200,
            'status'=>true,
            'data'=>$data
        ]);
    }


    public function GetAd(Request $request)
    {
        $list = Ad::where('type',$request->type)->orderBy('updated_at','desc')->first();
        if($list){
            $list['url'] = (Image::where('id',$list['url'])->first())['url'];
        }else{
            $list = [];
        }
        return $this->response->array([
            'code'=>200,
            'status'=>true,
            'data'=>$list
        ]);
    }
}
